﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Demo.TableStorageDataServices.Web.Models;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Demo.TableStorageDataServices.Web.Services
{
    public class DemoDataSource : BaseDataSource
    {
        public CloudStorageAccount CloudStorageAccount { get; private set; }
        private AzureServiceContext _azureServiceContext;

        private Enigma _currentEnigma;
        private Response _currentResponse;
        private User _currentUser;

        public DemoDataSource()
        {
            CloudStorageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnection");
            _azureServiceContext = GetServiceContext();
        }

        private AzureServiceContext GetServiceContext()
        {
            var serviceContext = new AzureServiceContext(CloudStorageAccount);
            return serviceContext;
        }

        public override IQueryable<Enigma> Enigmas
        {
            get { return _azureServiceContext.Enigmas; }
        }

        public override IQueryable<Response> Responses
        {
            get { return _azureServiceContext.Responses; }
        }

        public override IQueryable<User> Users
        {
            get { return _azureServiceContext.Users; }
        }

        public override object CreateResource(System.Data.Services.Providers.ResourceType resourceType)
        {

            if (resourceType.InstanceType == typeof(Enigma)) return new Enigma();
            if (resourceType.InstanceType == typeof(Response)) return new Response();
            if (resourceType.InstanceType == typeof(User)) return new User();

            throw new NotSupportedException(String.Format("{0} not found", resourceType.FullName));
        }

        public override void AddResource(System.Data.Services.Providers.ResourceType resourceType, object resource)
        {

            if (resourceType.InstanceType == typeof(Enigma))
            {
                var enigma = resource as Enigma;
                if (enigma != null)
                {
                    _currentEnigma = enigma;
                    return;
                }
            }
            if (resourceType.InstanceType == typeof(Response))
            {
                var response = resource as Response;
                if (response != null)
                {
                    _currentResponse = response;
                    return;
                }
            }
            if (resourceType.InstanceType == typeof(User))
            {
                var user = resource as User;
                if (user != null)
                {
                    _currentUser = user;
                    return;
                }
                System.Reflection.Assembly.LoadFile("");
            }
            throw new NotSupportedException("Type not found");
        }

        public override void DeleteResource(object resource)
        {
            if (resource.GetType() == typeof(Enigma))
            {
                return;
            }
            throw new NotSupportedException("Type not found");

        }

        public override void SaveChanges()
        {
            _azureServiceContext.AddEnigma(_currentEnigma);
            _azureServiceContext.AddResponse(_currentResponse);
            _azureServiceContext.AddUser(_currentUser);
        }
    }
}